package com.fyzk.activity.forum;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.tyjc.lottery.R;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.ScreenUtils;

/**
 * Created by fanxs11 on 17/5/6.
 */

public class HorizontalListView extends LinearLayout {
    private Context context;
    private GridView gridView;
    private int itemWidth = 80;

    public HorizontalListView(Context context) {
        super(context);
        initView(context);
    }

    public HorizontalListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HorizontalListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_horizontal_listview, null);

        gridView = (GridView) view.findViewById(R.id.horizontal_list_gv);
        this.addView(view);

    }

    public void setItemWidth(int itemWidth) {
        this.itemWidth = itemWidth;
    }

    public void setAdapter(BaseAdapter adapter) {
        int size = adapter.getCount();
        int length = itemWidth;
        float density = ScreenUtils.getInstance().getDensity();
        int gridviewWidth = (int) (size * (length + 4) * density);
        int itemWidth = (int) (length * density);

        LayoutParams params = new LayoutParams(
                gridviewWidth, LayoutParams.FILL_PARENT);
        gridView.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        gridView.setFocusable(true);
        gridView.setFocusableInTouchMode(true);
        gridView.setColumnWidth(itemWidth); // 设置列表项宽
        gridView.setHorizontalSpacing(5); // 设置列表项水平间距
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setNumColumns(size); // 设置列数量=列表集合数
        gridView.setAdapter(adapter);
    }

    public void notifyDataSetChanged(BaseAdapter adapter) {
        adapter.notifyDataSetChanged();
        int size = adapter.getCount();
        int length = itemWidth;
        float density = ScreenUtils.getInstance().getDensity();
        int gridviewWidth = (int) (size * (length + 4) * density);
        int itemWidth = (int) (length * density);

        LayoutParams params = new LayoutParams(
                gridviewWidth, LayoutParams.FILL_PARENT);
        gridView.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        gridView.setColumnWidth(itemWidth); // 设置列表项宽
        gridView.setHorizontalSpacing(5); // 设置列表项水平间距
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setNumColumns(size); // 设置列数量=列表集合数
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        if (gridView != null) {
            gridView.setOnItemClickListener(listener);
        }
    }
}
